var t;
var firsttel ='';
$(function(){
    //用户名
    $('#user').blur(function(){
        var nameReg = /^[\u4e00-\u9fff]{3,6}$/;
        var user=$.trim($('#user').val());
        if(user === '' || !(nameReg.test(user))){
            $('.user_hide').html('请输入3~6位中文字符!');
            $('.user_hide').css({"display":"inline-block"});
            $('.user_img').css({"display":"none"});
            userstatus=0;
        }else if(user.indexOf('毅思') >= 0){
            $('.user_hide').html('该用户名已经被注册过啦!');
            $('.user_hide').css({"display":"inline-block"});
            $('.user_img').css({"display":"none"});
            userstatus=0;
        }else{
            $.ajax({
                url:posturl,
                type: 'post',
                data: {username:user},
                dataType: "json",
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                success: function(res){
                    if(parseInt(res) === 1){
                         $('.user_hide').html('该用户名已经被注册过啦!');
                         $('.user_hide').css({"display":"inline-block"});
                         $('.user_img').css({"display":"none"});
                         userstatus=0;
                    }else{
                         $('.user_img').css("display","inline-block");
                         $('.user_hide').css("display","none");
                         userstatus=1;
                    }
                 }
            });
        }
        register();
    });
 
    //第一次密码
    $('#pwd_1').blur(function(){
       var pwd_1=$.trim($('#pwd_1').val());
       if(pwd_1.length >= 8){
          var pwd_2 = $.trim($('#pwd_2').val());
          if(pwd_2 != ''){
             if(pwd_1 != pwd_2){
                $('.pwd2_hide').html('两次密码不一致!');
                $('.pwd2_hide').css({"display":"inline-block"});
                $('.pwd2_img').css({"display":"none"});
                passStatus=0;
             }else{
                 $('.pwd1_hide').css({"display":"none"});
                 $('.pwd1_img').css({"display":"inline-block"});
                 $('.pwd2_hide').css({"display":"none"});
                 $('.pwd2_img').css({"display":"inline-block"});
                 passStatus=1;
             }
          }else{
              $('.pwd1_hide').css({"display":"none"});
              $('.pwd1_img').css({"display":"inline-block"});
              passStatus=1;
          }
          
       }else{
          $('.pwd1_hide').css({"display":"inline-block"});
          $('.pwd1_img').css({"display":"none"});
          passStatus=0;
       }
        register();
    });

    //第二次密码
    $('#pwd_2').blur(function(){
       var pwd_1 = $.trim($('#pwd_1').val());
       var pwd_2 = $.trim($('#pwd_2').val());
       if(pwd_1 != pwd_2){
          $('.pwd2_hide').html('两次密码不一致!');
          $('.pwd2_hide').css({"display":"inline-block"});
          $('.pwd2_img').css({"display":"none"});
          passStatus=0;
       }else if(pwd_1 == pwd_2 && pwd_1 != "" && pwd_2 != ""){
          if(pwd_1.length < 8){
            $('.pwd2_img').css({"display":"none"});
            $('.pwd2_hide').html('密码不能少于8位，建议字母数字组合!');
            $('.pwd2_hide').css({"display":"inline-block"});
            passStatus=0;
         }else{
             $('.pwd2_img').css({"display":"inline-block"});
             $('.pwd2_hide').css({"display":"none"});
             passStatus=1;
         }
       }
       register();
    });

    //手机号
    $('#moblie').blur(function(){
       var moblie = $.trim($('#moblie').val());
       if(!(/^(13|14|15|17|18)\d{9}$/.test(moblie))){
          $('.moblie_hide').html('请输入正确的手机号码!');
          $('.moblie_hide').css({"display":"inline"});
          $('.moblie_img').css({"display":"none"});
          $('.miao').removeAttr('onclick');
          $('.miao').css({"background":"#cad0e2","color":"#777"});
          telStatus=0;
          register();
       }else{
          $.ajax({
             url:telurl,
             type: 'post',
             data: {tel:moblie},
             headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
             success: function(res){
              if(parseInt(res) === 1){
                  $('.moblie_hide').css({"display":"inline-block"});
                  $('.moblie_hide').html('该手机号已被注册');
                 $('.miao').css({"background":"#cad0e2","color":"#777"});
                 $('.miao').removeAttr('onclick');
                 $('.moblie_img').css({"display":"none"});
                 $('.miao').css({'disabled':true});
                 verifyStatus=0; //为了避免先输入验证码 然后换成别的手机号
                 telStatus=0;
                 register();
              }else{
                  if((yanzhenma == 0) || (firsttel != moblie)){ //倒计时已结束 或者 用户填完手机验证码又修改了手机号
                      clearInterval(t);
                      $('.miao').attr("value", '获取验证码');
                      $('.miao').css({"background":"#00a8ee","color":"#fff"});
                      $('.moblie_hide').css({"display":"none"});
                      $('.moblie_img').css({"display":"inline-block"});
                      telStatus=1;
                      $('.miao').attr('onclick',"settime(this)");
                      $('.miao').css({'disabled':false});
                      $('.miao').css({"cursor":'pointer'});
                      $('.code_img').css({"display":"none"});
                      verifyStatus=0; //为了避免先输入验证码 然后换成别的手机号
                  }
                  register();
              }
             }
          });
       }
    });
    //邀请码
    $("#inviteCode").blur(function(){   //邀请码验证
        var inviteCode = $.trim($('#inviteCode').val());
        if(!(/^[0-9]{6}$/.test(inviteCode))){
           $('.invitecode_hide').css({"display":"inline"});
            $('.invite_img').css({"display":"none"});
            invitecodeStatu=0;
        }else{ 
           $('.invitecode_hide').css({"display":"none"});
            $('.invite_img').css({"display":"inline-block"});
            invitecodeStatu=1;
        }
        register();
    });
    //验证码
    $('#code').blur(function(){   //判断验证码
        var moblie = $.trim($('#moblie').val());
        if(!(/^(13|14|15|17|18)\d{9}$/.test(moblie))){
           $('.moblie_hide').css({"display":"inline"});
           $('.moblie_img').css({"display":"none"});
           $('.miao').css({"background":"#cad0e2","color":"#777"});
           verifyStatus=0;
           register();
        }else{
            var usercode=$.trim($('#code').val());
            if(usercode){
                $.ajax({
                    url:checkverifycode,
                    type: 'post',
                    data: {code:usercode},
                    dataType:'json',
                    headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                    success: function(res){
                        if(parseInt(res.status) == 1){ //正确
                            $('.code_img').css({"display":"inline-block"});
                            verifyStatus=1;
                            register();
                        }else if(parseInt(res.status) == 0){
                            layer.msg('请输入正确的验证码!');
                            $('.code_img').css({"display":"none"});
                            verifyStatus=0;
                            register();
                        }
                    }
                });
            }else{
                layer.msg('请输入验证码!');
                $('.code_img').css({"display":"none"});
                verifyStatus=0;
                register();
            }
        }
    });
    // 复选框
    $('.squar').click(function(){
      register();
    })
});
//发送手机验证码
function settime(obj) {
    var moblie=$.trim($('#moblie').val());
    if(!(/^(13|14|15|17|18)\d{9}$/.test(moblie))){
       $('.moblie_hide').css({"display":"inline-block"});
       $('.moblie_img').css({"display":"none"});
    }else{
        $('.miao').removeAttr("onclick");
        firsttel = moblie; //记录发送验证码的手机号
        $.ajax({
            url:sendIdentify,
            type: 'post',
            data: {tel:moblie},
            dataType:'json',
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            success: function(res){
                if(parseInt(res.status) == 0){
                   layer.tips('发送失败，请重新发送!', '#moblie');
                   $(obj).attr('onclick','settime(this)');
                }else if(parseInt(res.status) == 1){
                    yanzhenma = 1;
                    $(obj).removeAttr('onclick');
                    $(obj).css('background','#cad0e2');
                    $('#code').focus();

                    window.code=res.code;
                    var countdown=60;
                    var validCode=true;
                    if(validCode){
                        validCode = false;
                        t=setInterval(function () {
                        countdown--;
                        obj.value = countdown+"秒后重新发送";
                        $('.miao').removeAttr("onclick");
                        if (countdown == 0) {
                         yanzhenma = 0;
                         clearInterval(t);
                         obj.value="重新发送";
                         $(obj).css('background','#00a8ee');
                         $('.miao').attr("onclick", 'settime(this)');
                         validCode=true;
                        }
                       },1000);
                    }
                }
            }
        });
    }
}
//注册入库
function gotoRegister(){
    $('.four').removeAttr('onclick');
    if(userstatus == 1 && passStatus == 1 && telStatus == 1 && verifyStatus == 1 && invitecodeStatu == 1){
         var mydata=$("form[name=myform]").serialize();
           $.ajax({
                url:gotoregister,
                type: 'post',
                data: mydata,
                dataType:'json',
                success: function(res){
                    if(res.status){
                        //注册成功
                        window.location.href = gotourl;
                    }else{
                        //注册失败
                        window.location.reload();
                    }
                }
        });
    }
}
//注册验证
function  register() {
    var user = $.trim($('#user').val());
    var pwd_1 = $.trim($('#pwd_1').val());
    var pwd_2 = $.trim($('#pwd_2').val());
    var moblie = $.trim($('#moblie').val());
    var code = $.trim($('#code').val());
    var inviteCode = $.trim($('#inviteCode').val()); //邀请码
    var isChecked = $(".squar input[type='checkbox']").is(':checked');
    if(userstatus == 1 && passStatus == 1 && telStatus == 1 && verifyStatus == 1 && invitecodeStatu == 1 && isChecked == true){
        $('.four').css({"background":"#00a8ee","color":"#fff"});
        $('.four').attr("onclick", 'gotoRegister()');
    }else{
        $('.four').css({"background":"#cad0e2","color":"#777"});
        $('.four').removeAttr("onclick");
    }
}
